package 公司真题.shopee;

import 树.TreeNode;

import java.util.*;

public class _二叉树层序遍历 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String msg = sc.nextLine();
        msg = msg.substring(1,msg.length() - 1);
        String[] msgs = msg.split(",");
        Integer[] arr = new Integer[msgs.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = "#".equals(msgs[i]) ? null : Integer.valueOf(msgs[i]);
        }
        TreeNode root = TreeNode.string2Tree(arr);
        List<List<Integer>> res = cengxuOrder(root);
        // 对res进行格式化
        //System.out.println(res);
        format(res);
    }

    private static void format(List<List<Integer>> res) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < res.size(); i++) {
            if (i == res.size() - 1) {
                sb.append("[");
                List<Integer> temp = res.get(i);
                for (int j = 0; j < temp.size(); j++) {
                    if (j == temp.size() - 1) {
                        sb.append(temp.get(j));
                    } else {
                        sb.append(temp.get(j)).append(",");
                    }
                }
                sb.append("]");
            } else {
                sb.append("[");
                List<Integer> temp = res.get(i);
                for (int j = 0; j < temp.size(); j++) {
                    if (j == temp.size() - 1) {
                        sb.append(temp.get(j));
                    } else {
                        sb.append(temp.get(j)).append(",");
                    }
                }
                sb.append("]").append(",");
            }
        }
        sb.append("]");
        System.out.println(sb);
    }

    private static List<List<Integer>> cengxuOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> temp = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                temp.add(node.val);
            }
            res.add(temp);
        }
        return res;
    }

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int x) { val = x; }

        public static TreeNode string2Tree(Integer[] nums) {
            if (nums.length == 0) {
                return null;
            }
            TreeNode head = new TreeNode(nums[0]);
            LinkedList<TreeNode> subTree = new LinkedList<>();
            subTree.push(head);
            for (int i = 1; i < nums.length; i++) {
                if (!subTree.isEmpty()) {
                    TreeNode cur = subTree.pop();
                    if (nums[i] != null) {
                        cur.left = new TreeNode(nums[i]);
                        subTree.add(cur.left);
                    }
                    i++;
                    if (i >= nums.length) {
                        break;
                    }
                    if (nums[i] != null) {
                        cur.right = new TreeNode(nums[i]);
                        subTree.add(cur.right);
                    }

                } else {
                    break;
                }
            }
            return head;
        }
    }
}
